#region Using directives
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using webapp4U.BOL;
#endregion


namespace webapp4U.DAL
{
	 public class LogoDAL : BaseDAL
	 {
	     #region ObjIDataReader

	     /// <summary>
	     /// Returns a new Logo instance filled with the DataReader's current record data 
	     /// </summary>
	     private Logo GetLogoFromReader(IDataReader dr)
	     {
		     Logo obj = new Logo();
		     obj.LogoID = (dr["LogoID"] is DBNull) ? Int32.MinValue :  (Int32)dr["LogoID"];
		     obj.FileName = (dr["FileName"] is DBNull) ? String.Empty :  (String)dr["FileName"];
		     obj.URL = (dr["URL"] is DBNull) ? String.Empty :  (String)dr["URL"];
		     obj.RegionDate = (dr["RegionDate"] is DBNull) ? DateTime.MinValue :  (DateTime)dr["RegionDate"];
		     obj.ExpireDate = (dr["ExpireDate"] is DBNull) ? DateTime.MinValue :  (DateTime)dr["ExpireDate"];
		     obj.CateID = (dr["CateID"] is DBNull) ? Int32.MinValue :  (Int32)dr["CateID"];
		     obj.Position = (dr["Position"] is DBNull) ? Int32.MinValue :  (Int32)dr["Position"];
		     obj.DisplayOrder = (dr["DisplayOrder"] is DBNull) ? Int32.MinValue :  (Int32)dr["DisplayOrder"];
		     obj.AddedDate = (dr["AddedDate"] is DBNull) ? DateTime.MinValue :  (DateTime)dr["AddedDate"];
		     obj.IsActive = (dr["IsActive"] is DBNull) ? false :  (Boolean)dr["IsActive"];
		     return obj;
	     }
	     #endregion

	     /// <summary>
	     /// Returns a collection of Logo with the data read from the input DataReader
	     /// </summary>
	     private List<Logo> GetLogoCollectionFromReader(IDataReader dr)
	     {
		     List<Logo> obj = new List<Logo>();
		     while(dr.Read())
			     obj.Add(GetLogoFromReader(dr));
		     return obj;
	     }


         public List<Logo> GetLogoByPosition(Int32 Position)
         {
             using (SqlConnection myConn = GetConnectionString())
             {
                 SqlCommand myCommand = new SqlCommand("Job_Logo_GetLogoByPosition", myConn);
                 myCommand.CommandType = CommandType.StoredProcedure;
                 myCommand.Parameters.Add("@Position", SqlDbType.Int).Value = Position;
                 SqlDataReader dr = myCommand.ExecuteReader();
                 return GetLogoCollectionFromReader(dr);
             }
         }


	     public int Insert(Logo obj)
	     {
		     using (SqlConnection myConn = GetConnectionString())
		     {
		     SqlCommand myCommand = new SqlCommand("Job_Logo_Insert", myConn); 
		     myCommand.CommandType = CommandType.StoredProcedure; 
		     myCommand.Parameters.Add("@LogoID", SqlDbType.Int).Direction = ParameterDirection.Output;
		     myCommand.Parameters.Add("@FileName", SqlDbType.VarChar).Value = obj.FileName;
		     myCommand.Parameters.Add("@URL", SqlDbType.VarChar).Value = obj.URL;
		     myCommand.Parameters.Add("@RegionDate", SqlDbType.DateTime).Value = obj.RegionDate;
		     myCommand.Parameters.Add("@ExpireDate", SqlDbType.DateTime).Value = obj.ExpireDate;
		     myCommand.Parameters.Add("@CateID", SqlDbType.Int).Value = obj.CateID;
		     myCommand.Parameters.Add("@Position", SqlDbType.Int).Value = obj.Position;
		     myCommand.Parameters.Add("@DisplayOrder", SqlDbType.Int).Value = obj.DisplayOrder;
		     myCommand.Parameters.Add("@AddedDate", SqlDbType.DateTime).Value = obj.AddedDate;
		     myCommand.Parameters.Add("@IsActive", SqlDbType.Bit).Value = obj.IsActive;

		     int ret = myCommand.ExecuteNonQuery();
		     return (int)myCommand.Parameters["@LogoID"].Value; 
		     }
	     }

	     public bool Update(Logo obj)
	     {
		     using (SqlConnection myConn = GetConnectionString())
		     {
		     SqlCommand myCommand = new SqlCommand("Job_Logo_Update", myConn); 
		     myCommand.CommandType = CommandType.StoredProcedure; 
		     myCommand.Parameters.Add("@LogoID", SqlDbType.Int).Value = obj.LogoID;
		     myCommand.Parameters.Add("@FileName", SqlDbType.VarChar).Value = obj.FileName;
		     myCommand.Parameters.Add("@URL", SqlDbType.VarChar).Value = obj.URL;
		     myCommand.Parameters.Add("@RegionDate", SqlDbType.DateTime).Value = obj.RegionDate;
		     myCommand.Parameters.Add("@ExpireDate", SqlDbType.DateTime).Value = obj.ExpireDate;
		     myCommand.Parameters.Add("@CateID", SqlDbType.Int).Value = obj.CateID;
		     myCommand.Parameters.Add("@Position", SqlDbType.Int).Value = obj.Position;
		     myCommand.Parameters.Add("@DisplayOrder", SqlDbType.Int).Value = obj.DisplayOrder;
		     myCommand.Parameters.Add("@AddedDate", SqlDbType.DateTime).Value = obj.AddedDate;
		     myCommand.Parameters.Add("@IsActive", SqlDbType.Bit).Value = obj.IsActive;

		     int ret = myCommand.ExecuteNonQuery();
		     return (ret == 1);
		     }
	     }

	     public bool Delete(Int32 LogoID)
	     {
		     using (SqlConnection myConn = GetConnectionString())
		     {
		     SqlCommand myCommand = new SqlCommand("Job_Logo_Delete", myConn); 
		     myCommand.CommandType = CommandType.StoredProcedure; 
		     myCommand.Parameters.Add("@LogoID", SqlDbType.Int).Value = LogoID;

		     int ret = myCommand.ExecuteNonQuery();
		     return (ret == 1);
		     }
	     }

	     /// <summary>
	     /// Retrieves the Job_Logo with the specified ID
	     /// </summary>
	     public Logo GetLogoByID(Int32 LogoID)
	     {
		     using (SqlConnection myConn = GetConnectionString())
		     {
		     SqlCommand myCommand = new SqlCommand("Job_Logo_GetLogoByID", myConn); 
		     myCommand.CommandType = CommandType.StoredProcedure; 
		     myCommand.Parameters.Add("@LogoID", SqlDbType.Int).Value = LogoID;

		     SqlDataReader dr = myCommand.ExecuteReader();
		     return (dr.Read() ? GetLogoFromReader(dr) : null);
		     }
	     }

	     //Get All Logo
	     public List<Logo> GetAllLogo(int PageIndex, int PageSize)
	     {
		     using (SqlConnection myConn = GetConnectionString())
		     {
		     SqlCommand myCommand = new SqlCommand("Job_Logo_GetAllLogo", myConn); 
		     myCommand.CommandType = CommandType.StoredProcedure; 
		     myCommand.Parameters.Add("@PageIndex", SqlDbType.Int).Value = PageIndex;
		     myCommand.Parameters.Add("@PageSize", SqlDbType.Int).Value = PageSize;
		     SqlDataReader dr = myCommand.ExecuteReader();
		     return GetLogoCollectionFromReader(dr);
		     }
	     }

	     // Get Count All records of Table  Logo
	     public int GetCountLogo()
	     {
		     using (SqlConnection myConn = GetConnectionString())
		     {
		     SqlCommand myCommand = new SqlCommand("Job_Logo_GetCountLogo", myConn); 
		     myCommand.CommandType = CommandType.StoredProcedure; 
		     return (int)myCommand.ExecuteScalar();
		     }
	     }

	     // Get LogoByCateID
	     public List<Logo> GetLogoByCateID(Int32 CateID,int PageIndex, int PageSize)
	     {
		     using (SqlConnection myConn = GetConnectionString())
		     {
		     SqlCommand myCommand = new SqlCommand("Job_Logo_GetLogoByCateID", myConn); 
		     myCommand.CommandType = CommandType.StoredProcedure; 
		     myCommand.Parameters.Add("@CateID", SqlDbType.Int).Value = CateID;
		     myCommand.Parameters.Add("@PageIndex", SqlDbType.Int).Value = PageIndex;
		     myCommand.Parameters.Add("@PageSize", SqlDbType.Int).Value = PageSize;
		     SqlDataReader dr = myCommand.ExecuteReader();
		     return GetLogoCollectionFromReader(dr);
		     }
	     }

	     // Get LogoCountByCateID
	     public int GetLogoCountByCateID(Int32 CateID)
	     {
		     using (SqlConnection myConn = GetConnectionString())
		     {
		     SqlCommand myCommand = new SqlCommand("Job_Logo_GetLogoCountByCateID", myConn); 
		     myCommand.CommandType = CommandType.StoredProcedure; 
		     myCommand.Parameters.Add("@CateID", SqlDbType.Int).Value = CateID;
		     return (int)myCommand.ExecuteScalar();
		     }
	     }

	 }
}
